package com.ysy.tz.core.selfcomp;

import com.sun.javafx.scene.control.skin.DatePickerSkin;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Skin;
import javafx.util.StringConverter;
import javafx.util.converter.LocalDateStringConverter;

public class CustomerDatePicker extends DatePicker {
    private SimpleBooleanProperty showNow = new SimpleBooleanProperty(false);
    private SimpleBooleanProperty endTime = new SimpleBooleanProperty(false);
    static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    static StringConverter stringConverter;

    public CustomerDatePicker() {
        this.setConverter(stringConverter);
        this.showNow.addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                this.setValue(LocalDate.now());
            }

        });
    }

    public Date getDate() {
        Date date = Date.from(((LocalDate)this.getValue()).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        if (this.endTime.get()) {
            date = new Date(date.getTime() + 82800000L + 3540000L + 59000L);
        }

        return date;
    }

    public void resetToday() {
        this.setValue(LocalDate.now());
    }

    protected Skin<?> createDefaultSkin() {
        DatePickerSkin datePickerSkin = (DatePickerSkin)super.createDefaultSkin();
        datePickerSkin.getPopupContent().setStyle("-fx-font-size: 20px;");
        return datePickerSkin;
    }

    public boolean isShowNow() {
        return this.showNow.get();
    }

    public SimpleBooleanProperty showNowProperty() {
        return this.showNow;
    }

    public void setShowNow(boolean showNow) {
        this.showNow.set(showNow);
    }

    public boolean isEndTime() {
        return this.endTime.get();
    }

    public SimpleBooleanProperty endTimeProperty() {
        return this.endTime;
    }

    public void setEndTime(boolean endTime) {
        this.endTime.set(endTime);
    }

    static {
        stringConverter = new LocalDateStringConverter(dateTimeFormatter, (DateTimeFormatter)null);
    }
}
